using Microsoft.EntityFrameworkCore;
using SovaDataService.SovaModels;
using SovaDataService.SoModels;
using System.Collections.Generic;
using System.Linq;

namespace SovaDataService.SovaDataServices
{
    public class SearchDataService
    {
        /**
         * Retrieve one search.
         */
        public Search GetOne(int id)
        {
            using (var db = new DbContext())
            {
                return db.Searches
                .Where(s => s.Id == id)
                .SingleOrDefault();
            }
        }

        /**
         * Retrieve all the searches.
         */
        public IEnumerable<Search> GetAll()
        {
            using (var db = new DbContext())
            {
                IQueryable<Search> searches = db.Searches.Include(q => q.Account);
                return searches.ToList();
            }
        }

        /**
         * Retrieve all the searches for a specific user.
         */
        public IEnumerable<Search> GetAllForUser(int accountId)
        {
            using (var db = new DbContext())
            {
                IQueryable<Search> searches = db.Searches.Where(s => s.AccountId == accountId).Include(q => q.Account);
                return searches.OrderByDescending(s => s.SearchDate).ToList();
            }
        }


        /**
         * Retrieve the count of all searches for a specific user.
         */
        public int GetCount(int accountId)
        {
            using (var db = new DbContext())
            {
                return db.Searches.Where(s => s.AccountId == accountId).Count();
            }
        }
    }
}